package com.giveu.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * mybatis-plus代码生成器(用于生成entity)<br>
 * 注意:<br>
 *     因为没用mybatis-plus的Service和Controller所以生成完之后要删掉TTT目录
 * @author stylefeng
 * @Date 2017/5/21 12:38
 */
public class OracleGenerator {

	static String driverName = "oracle.jdbc.driver.OracleDriver";
	static String username = "dafy_sales2";
	static String password = "Test$20150508";
	static String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=FTdevdb02.dafycredit.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=devdb02)))";
	static String path = System.getProperty("user.dir")+"/target/GiveU.CZ";
	
//	static String driverName = "oracle.jdbc.driver.OracleDriver";
//	static String username = "DAFY_SALES";
//	static String password = "Ju$2017";
//	static String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=idcdbtest.dafycredit.com)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=dbtest01)))";


	public static void main(String[] args) {
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(path);//这里写你自己的java目录
		gc.setFileOverride(true);//是否覆盖
		gc.setActiveRecord(true);
		gc.setEnableCache(false);// XML 二级缓存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(false);// XML columList
		gc.setAuthor("*");
		gc.setMapperName("%sMapper");
		gc.setXmlName("%sMapper");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sController");
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.ORACLE);
		dsc.setTypeConvert(new OracleTypeConvert() {
			// 自定义数据库表字段类型转换【可选】
			@Override
			public DbColumnType processTypeConvert(String fieldType) {
				return super.processTypeConvert(fieldType);
			}
		});
		dsc.setDriverName(driverName);
		dsc.setUsername(username);
		dsc.setPassword(password);
		dsc.setUrl(url);
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		//strategy.setCapitalMode(true);
		//strategy.setTablePrefix(new String[]{"_"});// 此处可以修改为您的表前缀
		strategy.setInclude(new String[] { "CS_INSURANCE_LIST","CS_INSURANCE_LOG" }); // 需要生成的表
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		mpg.setStrategy(strategy);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.giveu.cunz.module");
		pc.setModuleName("business");
		pc.setEntity("model");
		pc.setMapper("dao");
		pc.setXml("dao.mapping");
		//		pc.setService("com.giveu.rqc.module.business.service");      
		//		pc.setServiceImpl("com.giveu.rqc.module.business.service.impl");   
		pc.setController("controller");   
		mpg.setPackageInfo(pc);
		pc.getEntity();
		

		// 注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				this.setMap(map);
			}
		};
		mpg.setCfg(cfg);
		
		// 自定义生成
		List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
		
		focList.add(new FileOutConfig("/templates/entity.enum.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/com/giveu/cunz/module/business/model/"+  tableInfo.getEntityPath() + "Enum.enum";
			}
		});
		
		focList.add(new FileOutConfig("/templates/vo.java.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/com/giveu/cunz/module/business/vo/"+  tableInfo.getEntityPath() + "VO.java";
			}
		});
		
		focList.add(new FileOutConfig("/templates/export.java.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/com/giveu/cunz/module/business/vo/"+  tableInfo.getEntityPath() + "Export.java";
			}
		});
		
		focList.add(new FileOutConfig("/templates/page.html.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/view/business/" + tableInfo.getEntityPath() + "/"+  tableInfo.getEntityPath() + ".html";
			}
		});
		focList.add(new FileOutConfig("/templates/page_add.html.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/view/business/" + tableInfo.getEntityPath() + "/"+  tableInfo.getEntityPath() + "_add.html";
			}
		});
		focList.add(new FileOutConfig("/templates/page_edit.html.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/view/business/" + tableInfo.getEntityPath() + "/"+  tableInfo.getEntityPath() + "_edit.html";
			}
		});
		
		focList.add(new FileOutConfig("/templates/page.js.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/static/module/business/" + tableInfo.getEntityPath() + "/"+  tableInfo.getEntityPath() + ".js";
			}
		});
		focList.add(new FileOutConfig("/templates/page_info.js.vm") {
			public String outputFile(TableInfo tableInfo) {
				return path + "/static/module/business/" + tableInfo.getEntityPath() + "/"+  tableInfo.getEntityPath() + "_info.js";
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		// 执行生成
		mpg.execute();

		// 打印注入设置
		System.err.println(mpg.getCfg().getMap().get("abc"));
	}

}